package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 套餐模块接口
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐模块接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true )
    public Result<String> upload(@RequestBody SetmealDTO setmealDTO)  {
        log.info("新增套餐：{}",setmealDTO.getName());
        setmealService.addSetmealInfo(setmealDTO);
        log.info("新增套餐成功：{}",setmealDTO.getName());
        return Result.success("新增成功");
    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> setmealPageQuery(SetmealPageQueryDTO setmealDTO)  {
        log.info("分页查询：{}",setmealDTO.getName());
        PageResult pageResult =  setmealService.setmealPageQuery(setmealDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getByID(@PathVariable Long id)  {
        log.info("根据id查询套餐：{}",id);
        SetmealVO setmealVO =  setmealService.getByID(id);
        return Result.success(setmealVO);
    }
    @DeleteMapping
    @ApiOperation("根据id批量删除套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true )
    public Result<String> getByID(@RequestParam List<Long> ids)  {
        log.info("根据id批量删除套餐：{}",ids);
        setmealService.deleteIds(ids);
        log.info("批量删除套餐成功" );
        return Result.success("删除成功");
    }
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true,key = "#p0.categoryId")
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO)  {
        log.info("修改套餐：{}",setmealDTO.getName());
        setmealService.updateSetmealInfo(setmealDTO);
        log.info("修改成功");
        return Result.success("修改成功");
    }
    @PostMapping("/status/{status}")
    @ApiOperation("修改状态")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result<String> updateStatus(@PathVariable Integer status,Long id)  {
        log.info("修改状态：{}",id);
        setmealService.updateStatus(status,id);
        log.info("修改成功");
        return Result.success("更改成功");
    }
}
